<?php

namespace app\admin\server;
use app\admin\model\Products as productsModel;

class Products 
{


	/**
	 * 查询商品列表数据
	 * @param  array  $where 查询条件
	 * @return [type]        [description]
	 */
	public function getProductsByWhere($where = [])
	{
		//获取提交数据
		$param = input('param.');
		//每页条数
        $limit = $param['pageSize'];
        $offset = ($param['pageNumber'] - 1) * $limit;
        //商品名查询
        if (!empty($param['searchName'])) {
            $where['products_name'] = ['like', '%' . $param['searchName'] . '%'];
        }
        //分类查询
        if (!empty($param['cate_id'])) {
            $where['cate_id'] = ['in', $param['cate_id']];
        } 
        $where['is_del'] = 0;
        //获取商品列表
        $products =  productsModel::where($where)->limit($offset,$limit)->order('o asc')->select();
        $products = $this->getNewResult($products);
        //商品总条数
        $count = productsModel::where($where)->count();

        return [$products,$count];
	}


	/**
	 * 假删除
	 * @return [type] [description]
	 */
	public function  delProduct()
	{
		$id = input('param.id');
		try{
		 	(new productsModel())->save(['is_del'=>1],['id'=>$id]);
		 	return msg(1, '', '操作成功');
		}catch(PDOException $e){
			return msg(-2, '', $e->getMessage());
		}
	}




	/**
	 * 上下架
	 */
	public function saveStatusByID()
	{
		$productsModel = new productsModel();
		$id = input('param.id');
		//查询status
		$status = productsModel::get($id)->is_show;

		$data['is_show'] = 1;
		if($status == 1){
		 	$data['is_show'] = 0;
		}
		try{
		 	$productsModel->save($data,['id'=>$id]);
		 	return msg(1, '', '操作成功');
		}catch(PDOException $e){
			return msg(-2, '', $e->getMessage());
		}
	}












	/**
	 * 数据重组
	 */
	private function getNewResult($selectResult)
    {
        foreach($selectResult as $key=>$vo){
            $selectResult[$key]['image'] = '<img src="' . $vo['image'] . '" width="40px" height="40px">';
            $selectResult[$key]['is_show'] = showOperate($this->isShow($vo['id'],$vo['is_show']));
            $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id']));
        }
        return $selectResult;
    }




    private function isShow($id,$is_show)
    {
    	if($is_show == 1){
            $button['下架'] = [
                'auth' => 'products/is_show',
                'href' => "javascript:is_show(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-long-arrow-down'
            ];
        }else{
             $button['上架'] = [
                'auth' => 'products/is_show',
                'href' => "javascript:is_show(" . $id . ")",
                'btnStyle' => 'primary',
                'icon' => 'fa fa-long-arrow-up'
            ];
        }
        return $button;
    }

	/**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private  function makeButton($id)
    {
        $button = [
            '编辑' => [
                'auth' => 'products/update',
                'href' => url('product/productupdate', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '加入回收站' => [ 
                'auth' => 'products/del',
                'href' => "javascript:del(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ],
            
   
        ];

        return $button;
    }
}